//
//  WZProcedureController.m
//  ETong
//
//  Created by wanzhao on 15/10/29.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZProcedureController.h"
#import "WZMenuView.h"
#import "WZTabBarButton.h"
#import "WZProcedure.h"
#import "CContactModel.h"
#import "CFolderMgr.h"
#import "CContactMgr.h"
#import "MJExtension.h"
#import "WZProcedureDetail.h"
#import "WZProcedureCell.h"
#import "WZWebViewController.h"
#import "WZProcedureLogsController.h"
#import "WZProcedureTimeCell.h"
#import "UIBarButtonItem+category.h"
#import "MBProgressHUD+WZ.h"
#import "NetWorkShare.h"
#import "WZProcedureDraft.h"
#import "MJRefresh.h"
#import "CustomPopOverView.h"
#import "WZWorkFlowListVC.h"
#import "WZApplyList.h"

#define WZProcedureCategory self.leftTableView.menuDataArray[self.leftTableView.indexPathForSelectedRow.row]
#define ProcedureStatus @"非运行状态!"

@interface WZProcedureController ()<WZMenuViewDelegate, UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate>


@property (nonatomic, weak) WZMenuView *leftTableView;
@property (nonatomic, weak) UITableView *rightTableView;
@property (nonatomic, weak) UIView *leftLine;
@property (nonatomic, weak) UILabel *badgeLabel;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *listArray;

@property (nonatomic, weak) WZTabBarButton *seletedBtn;
@property (nonatomic, weak) WZTabBarButton *zankai;
@property (nonatomic, weak) UIImageView *bottomZankaiView;

@property (nonatomic, weak) UIImageView *bottomJuxingBeijing;
@property (nonatomic, weak) UIImageView *bottomTUqiBeijing;
@property (nonatomic, weak) UIImageView *badgeView;


@property (nonatomic, strong) CAccountModel *curAccount;
@property (nonatomic, copy) NSString *applyUrl;
@property (nonatomic, copy) NSString *viewUrl;
@property (nonatomic, copy) NSString *dealUrl;
@property (nonatomic, copy) NSString *yibanLiuChengUrl;
@property (nonatomic, copy) NSString *delUrl;
@property (nonatomic, copy) NSString *editUrl;
@property (nonatomic, copy) NSString *selectedCategory;
@property (nonatomic, assign) NSInteger finishedPage;
@property (nonatomic, assign) NSInteger badgeNum;
/** 左边的类别数据 */
@property (nonatomic, assign) NSInteger total;

/**OA用户名*/
@property (nonatomic, copy) NSString *oaUserName;
/**OA 登陆密码*/
@property (nonatomic, copy) NSString *oaPwd;
/**OA userId*/
@property (nonatomic, copy) NSString *oaUserId;
/**OA 公司ID*/
@property (nonatomic, copy) NSString *oaCompanyID;
/**OA 姓名*/
@property (nonatomic, copy) NSString *oaName;

@end

@implementation WZProcedureController

- (void)dealloc
{
    NSLog(@"WZProcedureController  dealloc");
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupUI];
    
    [self addChildControl];
    
    [self fetchPendingData];
}

- (NSString *)oaUserName {
    
    if (_oaUserName) {
        
        return [_oaUserName stringByReplacingOccurrencesOfString:@"@" withString:@"_"];
    } else {
        
        return nil;
    }
}

- (void)setupUI {
    
    self.view.backgroundColor = WhiteColor;
    self.navigationItem.title = @"流程申请";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"shuaxin" title:nil target:self sel:@selector(shuaXinShuJu) titleFont:Font10 titleColor:WhiteColor];
    
    self.curAccount = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    //    self.curAccount = [[[CContactMgr alloc] init] loadContactWithUserCode:acc.userName];
    
    self.oaUserName = [USER_DEFAULT objectForKey:@"oaUserName"];
    self.oaCompanyID = [USER_DEFAULT objectForKey:@"oaCompanyID"];
    self.oaPwd = [USER_DEFAULT objectForKey:@"oaPwd"];
    
}


// 初始化刷新控件
- (void)setupRefreshWithBtn:(WZTabBarButton *)btn {
    
    if (btn.tag == 30001) {
        
        self.rightTableView.footer = nil;
        self.rightTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreWorkflowProcedure:)];
        
    } else if (btn.tag == 30002) {
        
        self.rightTableView.footer = nil;
        self.rightTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAppliedProcedure:)];
        
    } else if (btn.tag == 30003) {
        
        self.rightTableView.footer = nil;
        self.rightTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAppendingProcedure:)];
        
    } else if (btn.tag == 30004) {
        
        self.rightTableView.footer = nil;
        self.rightTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFinishedProcedure:)];
        
    } else if (btn.tag == 30007) {
    
        self.rightTableView.footer = nil;
        self.rightTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDraftBoxProcedure:)];
    }
    
//    self.rightTableView.footer.hidden = YES;
}

- (void)checkFooterStateWithBtn:(WZTabBarButton *)btn {
    
    switch (btn.tag) {
        case 30001:
            
            
            break;
            
        case 30002:
            
            
            break;
            
        case 30003:
            
            
            break;
            
        case 30004:
            
            
            break;
            
        case 30007:
            
            
            break;
            
        default:
            break;
    }
    
    // 每次刷新右边数据时, 都控制footer显示或者隐藏
//    self.rightTableView.footer.hidden = (self.total == 0);
    
    if (self.dataArray.count >= self.total) {
        [self.rightTableView.footer endRefreshingWithNoMoreData];
    } else {
        [self.rightTableView.footer endRefreshing];
    }
}


- (void)loadMoreWorkflowProcedure:(NSString *)name {
    
    if (_dataArray.count <= 20) {
        
        [self.rightTableView.footer endRefreshingWithNoMoreData];
//        [self.rightTableView.footer setHidden:YES];
        return;
    }
    
    if (self.selectedCategory == nil) {
        name = @"";
    } else {
        
        name = self.selectedCategory;
    }
    
    [NetWorkShare workFlowApplyWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:@"1" pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                if (name.length <= 0) {
                    NSArray *array = [WZProcedure objectArrayWithKeyValuesArray:responseObject[@"categorys"]];
                    if (array.count > 0) {
                        WZProcedure *pro = array.firstObject;
                        pro.isSelected = YES;
                        self.leftTableView.menuDataArray = array;
                        self.leftLine.hidden = NO;
                        self.leftTableView.hidden = NO;
                    } else {
                        self.leftLine.hidden = YES;
                        self.leftTableView.hidden = YES;
                    }
                }
                
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                
                // 判断是否最后一次请求
                
                [self.rightTableView reloadData];
                self.applyUrl = responseObject[@"applyUrl"];
                
                [self checkFooterStateWithBtn:self.seletedBtn];
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}


- (void)loadMoreAppliedProcedure:(NSString *)name {
    
    WZProcedure *cate = WZProcedureCategory;
    if (self.selectedCategory == nil) {
        name = cate.name;
        self.total = cate.total;
    } else {
        
        name = self.selectedCategory;
    }
    
    NSString *pageNum = [NSString stringWithFormat:@"%@", @(++cate.currentPage + 1)];
    [NetWorkShare yiShengQingLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:pageNum pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                if (name.length <= 0) {
                    NSArray *array = [WZProcedure objectArrayWithKeyValuesArray:responseObject[@"categorys"]];
                    if (array.count > 0) {
                        WZProcedure *pro = array.firstObject;
                        pro.isSelected = YES;
                        self.leftTableView.menuDataArray = array;
                        self.leftLine.hidden = NO;
                        self.leftTableView.hidden = NO;
                    } else {
                        self.leftLine.hidden = YES;
                        self.leftTableView.hidden = YES;
                    }
                }
                
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                
                self.viewUrl = responseObject[@"viewUrl"];
                
                [self checkFooterStateWithBtn:nil];
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

- (void)loadMoreAppendingProcedure:(NSString *)name {
    
    name = @"";
    [NetWorkShare daiBanLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:@"2" pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                self.dealUrl = responseObject[@"dealUrl"];
                
                [self checkFooterStateWithBtn:self.seletedBtn];
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

- (void)loadMoreFinishedProcedure:(NSString *)name {
    
    name = @"";
//    self.leftTableView.hidden = YES;
    NSString *pageNum = [NSString stringWithFormat:@"%@",@(++self.finishedPage)];
    [NetWorkShare yiBanLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:pageNum pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                
                self.yibanLiuChengUrl = responseObject[@"viewUrl"];
                
                if ([responseObject objectForKey:@"datas"] == nil) {
                    
                    [self.rightTableView.footer endRefreshingWithNoMoreData];
//                    self.rightTableView.footer.hidden = YES;
                } else {
                    [self.rightTableView.footer endRefreshingWithNoMoreData];
                }
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

- (void)loadMoreDraftBoxProcedure:(NSString *)name {
    
    if (_dataArray.count <= 20) {
        
        [self.rightTableView.footer endRefreshingWithNoMoreData];
//        [self.rightTableView.footer setHidden:YES];
        return;
    }
    name = @"";
    [NetWorkShare caoGaoXiangWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:@"2" pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                
                [self.dataArray addObjectsFromArray:[WZProcedureDraft objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                
                self.delUrl = responseObject[@"delUrl"];
                self.editUrl = responseObject[@"editUrl"];
                
                if ([responseObject objectForKey:@"datas"] == nil) {
                    
                    [self.rightTableView.footer endRefreshingWithNoMoreData];
//                    self.rightTableView.footer.hidden = YES;
                } else {
                    [self.rightTableView.footer endRefreshingWithNoMoreData];
                }
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

#pragma mark 流程申请
- (void)loadWorkflowDataWithName:(NSString *)name
{
    
//    if (M_ISSTRNULL(self.curAccount.userName) || M_ISSTRNULL(self.curAccount.passWord) || M_ISSTRNULL(self.curAccount.companyId)) {
//        
//        return;
//    }
    
    if (M_ISSTRNULL(self.oaUserName) || M_ISSTRNULL(self.oaPwd) || M_ISSTRNULL(self.oaCompanyID)) {
        
        return;
    }
    
    [MBProgressHUD showMessage:@"加载中，请稍候..." toView:self.view];
    [NetWorkShare workFlowApplyWithLoginName:self.curAccount.userName password:self.curAccount.passWord companyId:self.curAccount.companyId pageNo:@"1" pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            [MBProgressHUD hideHUDForView:self.view];
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                if (name.length <= 0) {
                    NSArray *array = [WZProcedure objectArrayWithKeyValuesArray:responseObject[@"categorys"]];
                    if (array.count > 0) {
                        WZProcedure *pro = array.firstObject;
                        pro.isSelected = YES;
                        self.leftTableView.menuDataArray = array;
                        self.leftLine.hidden = NO;
                        self.leftTableView.hidden = NO;
                    } else {
                        self.leftLine.hidden = YES;
                        self.leftTableView.hidden = YES;
                    }
                }
                
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                self.applyUrl = responseObject[@"applyUrl"];
                
                [self checkFooterStateWithBtn:self.seletedBtn];
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}


#pragma mark 已申请流程
- (void)loadYiShenQingDataWithName:(NSString *)name
{
    [MBProgressHUD showMessage:@"加载中，请稍候..." toView:self.view];
    
    WZProcedure *cate = WZProcedureCategory;
    cate.currentPage = 1;
    NSString *pageNum =[NSString stringWithFormat:@"%ld", cate.currentPage];
    [NetWorkShare yiShengQingLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:pageNum pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                if (name.length <= 0) {
                    NSArray *array = [WZProcedure objectArrayWithKeyValuesArray:responseObject[@"categorys"]];
                    if (array.count > 0) {
                        WZProcedure *pro = array.firstObject;
                        pro.isSelected = YES;
                        self.leftTableView.menuDataArray = array;
                        self.leftLine.hidden = NO;
                        self.leftTableView.hidden = NO;
                    } else {
                        self.leftLine.hidden = YES;
                        self.leftTableView.hidden = YES;
                    }
                    
                }
                
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                
                self.viewUrl = responseObject[@"viewUrl"];
                
//                [self checkFooterStateWithBtn:self.seletedBtn];
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

#pragma mark 待办流程
- (void)loadDaiBanLiuChengDataWithName:(NSString *)name
{
    [MBProgressHUD showMessage:@"加载中，请稍候..." toView:self.view];
    [NetWorkShare daiBanLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:@"1" pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
//            NSString * str = [self transformJsonStrWithObject:responseObject[@"datas"]];
//            WZLog(@"---------刷新的数据 Data= %@", str);
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                self.dealUrl = responseObject[@"dealUrl"];
                
                
                if (!_dataArray.count) {
                    self.badgeView.hidden = YES;
                } else {
                    self.badgeLabel.text = _dataArray.count > 9 ? [NSString stringWithFormat:@"%@", @"9+"] : [NSString stringWithFormat:@"%zd", _dataArray.count];
                    self.badgeView.hidden = NO;
                }
//                [self checkFooterStateWithBtn:self.seletedBtn];
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
       [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

#pragma mark 已办流程
- (void)loadYiBanLiuChengDataWithName:(NSString *)name
{
    WZLog(@"finishedProcedure name = %@", name);
    [MBProgressHUD showMessage:@"加载中，请稍候..." toView:self.view];
    
    self.finishedPage = 1;
    NSString *pageNum = [NSString stringWithFormat:@"%@", @(self.finishedPage)];
    
    [NetWorkShare yiBanLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:pageNum pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:[WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                
                self.yibanLiuChengUrl = responseObject[@"viewUrl"];
                
//                [self checkFooterStateWithBtn:self.seletedBtn];
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

#pragma mark 草稿箱
- (void)loadCaoGaoXiangDataWithName:(NSString *)name
{
    [MBProgressHUD showMessage:@"加载中，请稍候..." toView:self.view];
    [NetWorkShare caoGaoXiangWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:@"1" pageSize:@"20" category:name successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:[WZProcedureDraft objectArrayWithKeyValuesArray:responseObject[@"datas"]]];
                [self.rightTableView reloadData];
                
                self.delUrl = responseObject[@"delUrl"];
                self.editUrl = responseObject[@"editUrl"];
                
                [self checkFooterStateWithBtn:self.seletedBtn];
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!IsIOS7) {
        self.navigationController.navigationBar.tintColor = RGBCOLORWITH16COLOR(0xa694d4);
    } else {
        self.navigationController.navigationBar.barTintColor = RGBCOLORWITH16COLOR(0xa694d4);
    }
    
    if (self.seletedBtn.tag == 30003) {
        [self shuaXinShuJu];
    }
}

- (WZTabBarButton *)addtableBarButtonWithImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName title:(NSString *)title
{
    WZTabBarButton *btn = [WZTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = Font10;
    [btn setTitleColor:RGBCOLORWITH16COLOR(0xac92ed) forState:UIControlStateSelected];
    [btn setTitleColor:RGBCOLORWITH16COLOR(0xa8a8a8) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

#pragma mark -- 导航栏刷新按钮
- (void)shuaXinShuJu
{
//    [self menueViewDidSelectWithName: self.selectedCategory];
    [self menueViewDidSelectWithName:self.selectedCategory total:0];
}


#pragma mark -- 底部按钮点击事件
- (void)btnClick:(WZTabBarButton *)btn
{
    // 结束刷新
    [self.rightTableView.footer endRefreshing];
    
    self.selectedCategory = nil;
    if (btn.tag != 30005 && btn.tag != 30008) {
        self.seletedBtn.selected = NO;
        btn.selected = !btn.selected;
        self.seletedBtn = btn;
    }
    
    switch (btn.tag) {
        case 30001:
        {
            [self applyForProcedure];
//            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        }
            break;
        case 30002:
        {
            [self appliedProcedure];
        }
            break;
        case 30003:
        {
            [self pendingProcedure];
        }
            break;
        case 30004:
        {
            [self finishProcedureDidClick];
        }
            break;
        case 30005:
        {
            [self showDraftBox];
        }
            break;
        case 30006:
        {
            // 已取消
        }
            break;
        case 30007:
        {
            [self draftBoxDidClick];
        }
            break;
        case 30008:
        {
            [self hiddenDraftBox];
        }
            break;

        default:
            break;
    }
    
    if (btn.tag == 30001 || btn.tag == 30002 || btn.tag == 30003 || btn.tag == 30004 || btn.tag == 30007) {
        
        [self setupRefreshWithBtn:btn];
        
    } else {
        
//        self.rightTableView.footer.hidden = YES;
    }
}

// 流程申请
- (void)applyForProcedure {
    
    self.navigationItem.title = @"流程申请";
    [self hiddenDraftBox];
    self.leftTableView.hidden = YES;
    self.leftLine.hidden = YES;
    self.rightTableView.frame = CGRectMake(CGRectGetMaxX(self.leftTableView.frame), 0, self.view.width - CGRectGetMaxX(self.leftTableView.frame), HARDWARE_SCREEN_HEIGHT - 64.0f - OppositeHeightRate(72));
    [self.dataArray removeAllObjects];
    [self.rightTableView reloadData];
    
    [self loadWorkflowDataWithName:nil];
}

// 已申请流程
- (void)appliedProcedure {
    
    self.navigationItem.title = @"已申请流程";
    [self hiddenDraftBox];
    self.leftTableView.hidden = YES;
    self.leftLine.hidden = YES;
    self.rightTableView.frame = CGRectMake(CGRectGetMaxX(self.leftTableView.frame), 0, self.view.width - CGRectGetMaxX(self.leftTableView.frame), HARDWARE_SCREEN_HEIGHT - 64.0f - OppositeHeightRate(72));
    [self.dataArray removeAllObjects];
    [self.rightTableView reloadData];
    
    [self loadYiShenQingDataWithName:nil];
}

// 待办流程
- (void)pendingProcedure {
    
    self.navigationItem.title = @"待办流程";
    [self hiddenDraftBox];
    self.leftTableView.hidden = YES;
    self.leftLine.hidden = YES;
    self.rightTableView.frame = CGRectMake(0, 0, self.view.width, HARDWARE_SCREEN_HEIGHT - 64.0f - OppositeHeightRate(72));
    [self.dataArray removeAllObjects];
    [self.rightTableView reloadData];
    [self loadDaiBanLiuChengDataWithName:nil];
}


// 已办流程
- (void)finishProcedureDidClick {
    
    self.navigationItem.title = @"已办流程";
    [self hiddenDraftBox];
    self.leftTableView.hidden = YES;
    self.leftLine.hidden = YES;
    self.rightTableView.frame = CGRectMake(0, 0, self.view.width, HARDWARE_SCREEN_HEIGHT - 64.0f - OppositeHeightRate(72));
    [self.dataArray removeAllObjects];
    [self.rightTableView reloadData];
    [self loadYiBanLiuChengDataWithName:nil];
}

// 点击草稿箱
- (void)draftBoxDidClick {
    
    self.navigationItem.title = @"草稿箱";
    [self hiddenDraftBox];
    self.leftTableView.hidden = YES;
    self.leftLine.hidden = YES;
    self.rightTableView.frame = CGRectMake(0, 0, self.view.width, HARDWARE_SCREEN_HEIGHT - 64.0f - OppositeHeightRate(72));
    [self.dataArray removeAllObjects];
    [self.rightTableView reloadData];
    [self loadCaoGaoXiangDataWithName:nil];
    
}

// 显示草稿箱
- (void)showDraftBox {
    
    self.zankai.hidden = YES;
    self.bottomTUqiBeijing.hidden = YES;
    self.bottomJuxingBeijing.hidden = NO;
    
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        
        self.bottomZankaiView.y = HARDWARE_SCREEN_HEIGHT - 64 - OppositeHeightRate(72) - OppositeHeightRate(50);
        
    } completion:^(BOOL finished) {
        
    }];
}


// 隐藏草稿箱
- (void)hiddenDraftBox {
    
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.bottomZankaiView.y = HARDWARE_SCREEN_HEIGHT - 64 - OppositeHeightRate(72);
    } completion:^(BOOL finished) {
        self.zankai.hidden = NO;
        self.bottomTUqiBeijing.hidden = NO;
        self.bottomJuxingBeijing.hidden = YES;
    }];
}

- (void)yinchangBottomZanKaiView
{
   
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
         self.bottomZankaiView.y = HARDWARE_SCREEN_HEIGHT - 64 - OppositeHeightRate(72);
    } completion:^(BOOL finished) {
        self.zankai.hidden = NO;
        self.bottomTUqiBeijing.hidden = NO;
        self.bottomJuxingBeijing.hidden = YES;
    }];
}

#pragma menueView 左侧tableView 代理方法
- (void)menueViewDidSelectWithName:(NSString *)name total:(NSInteger)total
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    //结束刷新
    [self.rightTableView.footer endRefreshing];
    
    self.selectedCategory = name;
    self.total = total;
    switch (self.seletedBtn.tag) {
        case 30001:
        {
            [self loadWorkflowDataWithName:name];
        }
            break;
        case 30002:
        {
            [self loadYiShenQingDataWithName:name];
        }
            break;
        case 30003:
        {
            [self loadDaiBanLiuChengDataWithName:name];
        }
            break;
        case 30004:
        {
            [self loadYiBanLiuChengDataWithName:name];
        }
            break;
        case 30006:
        {
            
        }
            break;
        case 30007:
        {
            [self loadCaoGaoXiangDataWithName:name];
        }
            break;
            
        default:
            break;
    }
}

- (NSArray *)rightButtons
{
    
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    
    if (self.seletedBtn.tag == 30002 || self.seletedBtn.tag == 30003 || self.seletedBtn.tag == 30004) {
        [rightUtilityButtons sw_addUtilityButtonWithColor:RGBCOLOR(230, 96, 184) title:@"查看日志"];
    } else if(self.seletedBtn.tag == 30007) {
        [rightUtilityButtons sw_addUtilityButtonWithColor:RGBCOLOR(248, 91, 92) icon:[UIImage imageNamed:@"shanchu"]];
    } else {
        
    }
    return rightUtilityButtons;
}


#pragma mark tableView相关方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return OppositeHeightRate(50);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (self.seletedBtn.tag == 30001 || self.seletedBtn.tag == 30002) {
        WZProcedureCell *proCell = [WZProcedureCell procedureCellWithTableView:tableView];
        WZProcedureDetail *pro = self.dataArray[indexPath.row];
        proCell.rightUtilityButtons = [self rightButtons];
        proCell.delegate = self;
        proCell.procedureDetail = pro;
        cell = proCell;
    } else if (self.seletedBtn.tag == 30004 || self.seletedBtn.tag == 30003){
        
        WZProcedureTimeCell *timeCell = [WZProcedureTimeCell procedureTimeCellWithTableView:tableView];
        WZProcedureDetail *pro = self.dataArray[indexPath.row];
        timeCell.procedureDetail = pro;
        
        timeCell.rightUtilityButtons = [self rightButtons];
        
        timeCell.delegate = self;
        cell = timeCell;
        
    } else if (self.seletedBtn.tag == 30007) {
        
        WZProcedureTimeCell *timeCell = [WZProcedureTimeCell procedureTimeCellWithTableView:tableView];
        WZProcedureDraft *draft = self.dataArray[indexPath.row];
        
//        WZProcedureDetail *pro = self.dataArray[indexPath.row];
//        timeCell.procedureDetail = pro;
        
        timeCell.rightUtilityButtons = [self rightButtons];
        timeCell.delegate = self;
        timeCell.procedurreDraft = draft;
        cell = timeCell;
        
    } else {
        
        WZLog(@"selected other Button");
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WZProcedureDetail *detail = self.dataArray[indexPath.row];
    
    if (!tableView.isEditing) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    if (self.seletedBtn.tag == 30001) {

        if (tableView == self.leftTableView) {
            
            [tableView reloadData];
            
        } else { // 流程申请右边的tableView
            
            [MBProgressHUD showMessage:@"正在加载..." toView:self.view];
            [self didClickApplyWrokFlowRightTableView:tableView DetailProcedure:detail IndexPath:indexPath];
        }
         
    } else {
        
        [self.dataArray enumerateObjectsUsingBlock:^(WZProcedureDetail *obj, NSUInteger idx, BOOL *stop) {
            if (indexPath.row == idx) {
                obj.isSelected = YES;
            } else {
                obj.isSelected = NO;
            }
        }];
        
        [self.rightTableView reloadData];
        
        NSString *urlStr = nil;
        switch (self.seletedBtn.tag) {
                
            case 30001:
            {
                // 申请流程
                WZLog(@"申请流程");
            }
                break;
                
            case 30002:
            {
                // 已申请
                if ([[detail runStatus] isEqualToString:@"运行中"]) {
                    
                    urlStr = [[NSString stringWithFormat:@"http://%@/OA_CBD%@&formid=%@&processInstanceId=%@&mversion=%@&taskdef=%@&fversion=%@&dataid=%@",SERVERIP, self.viewUrl, detail.formId, detail.procInsId, detail.mversion, detail.taskDef, detail.fversion, detail.formTableId] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    WZLog(@"webUrl = %@", urlStr);
                    
                } else {
                    
                    [MBProgressHUD showError:ProcedureStatus];
                    return;
                }
                
            }
                break;
            case 30003:
            {
                // 待办
                if ([[detail runStatus] isEqualToString:@"已接收"] || [[detail runStatus] isEqualToString:@"未接收"]) {
                
                    urlStr = [[NSString stringWithFormat:@"http://%@/OA_CBD%@&dataid=%@&taskId=%@&processInstanceId=%@&workFlowflag=%@&mversion=%@&taskDefKey=%@&formid=%@&fversion＝%@&isClaim=%@",SERVERIP, self.dealUrl,detail.formTableId,detail.taskId,detail.procInsId,detail.modelId,detail.mversion,detail.taskDef,detail.formId,detail.fversion,detail.claim?@"true":@"false"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                } else {
                    
                    [MBProgressHUD showError:ProcedureStatus];
                    return;
                }
            }
                break;
            case 30004:
            {
                // 已办
                if ([[detail runStatus] isEqualToString:@"运行中"]) {
                    
                    urlStr = [[NSString stringWithFormat:@"http://%@/OA_CBD%@&dataid=%@&formid=%@&mversion=%@&taskdef=%@&fversion=%@&processInstanceId=%@",SERVERIP, self.yibanLiuChengUrl,detail.formTableId,detail.formId,detail.mversion,detail.taskDef,detail.fversion, detail.procInsId] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    
                } else {
                    
                    [MBProgressHUD showError:ProcedureStatus];
                    return;
                }
            }
                break;
            case 30006:
            {
                
            }
                break;
            case 30007:
            {
                // 点击草稿箱列表
                [self didSelectDraftList:indexPath];
                
                return;
            }
                break;
                
            default:
                break;
        }
        
        
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
        NSString *autho = [[[NSString stringWithFormat:@"%@:%@:%@",self.oaUserName,self.oaPwd,self.oaCompanyID] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
        [req addValue:[NSString stringWithFormat:@"Basic %@",autho] forHTTPHeaderField:@"Authorization"];
        
        NSURLRequest *request = [req copy];
        WZWebViewController *web = [[WZWebViewController alloc] init];
        web.proDetail = detail;
        web.request = request;
        [self.navigationController pushViewController:web animated:YES];
    }
}


#pragma mark -- 点击流程申请
- (void)didClickApplyWrokFlowRightTableView:(UITableView *)tableView DetailProcedure:(WZProcedureDetail *)detail IndexPath:(NSIndexPath *)indexPath {
    
    [NetWorkShare getWorkFlowWithApplyAndLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID FormId:detail.formId successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            WZLog(@"result = %@", responseObject);
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                [MBProgressHUD hideHUDForView:self.view];
                [self.listArray removeAllObjects];
                self.listArray = [WZApplyList objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                
                self.applyUrl = responseObject[@"applyUrl"];
                
                WZLog(@"count = %lu", (unsigned long)self.listArray.count);
                NSNumber *num = [NSNumber numberWithUnsignedInteger:self.listArray.count];
                [[NSNumber numberWithUnsignedInteger:self.listArray.count] isEqualToNumber:@(0)];
                WZLog(@"%@", num);
                
                if (self.listArray.count > 1) {
                    
                    WZWorkFlowListVC *applyVc = [[WZWorkFlowListVC alloc] init];
                    applyVc.formId = detail.formId;
                    applyVc.oaUserName = self.oaUserName;
                    applyVc.oaPwd = self.oaPwd;
                    applyVc.oaCompanyID = self.oaCompanyID;
                    applyVc.oaUserName = self.oaUserName;
                    [self.navigationController pushViewController:applyVc animated:YES];
                } else if (self.listArray.count == 1 && [[self.listArray.firstObject runState] isEqualToString:@"运行中"]) {
                    
                    NSString *applyURLStr = [[NSString stringWithFormat:@"http:%@/OA_CBD%@&formid=%@&workFlowflag=%@&mversion=%@&taskdef=%@&fversion=%@",SERVERIP, self.applyUrl, [self.listArray.firstObject formKey], [self.listArray.firstObject modelId], [self.listArray.firstObject mversion], [self.listArray.firstObject startEventKey], [self.listArray.firstObject fversion]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    
                    NSString *autho = [[[NSString stringWithFormat:@"%@:%@:%@", self.oaUserName, self.oaPwd, self.oaCompanyID] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
                    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:applyURLStr]];
                    
                    [req addValue:[NSString stringWithFormat:@"Basic %@", autho] forHTTPHeaderField:@"Authorization"];
                    
                    NSURLRequest *request = [req copy];
                    WZWebViewController *web = [[WZWebViewController alloc] init];
                    web.proDetail = detail;
                    web.request = request;
                    
                    [self.navigationController pushViewController:web animated:YES];
                }
                
            }  else if ([responseObject objectForKey:@"datas"] == nil) {
                
                WZLog(@"这是一种逆天的情况!");
            }
        } else {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showError:UNKNOWNERROR toView:self.view];
        }
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        WZLog(@"error = %@", error);
        
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:NETWORKNERROR toView:self.view];
    }];
}


// 点击草稿箱列表
- (void)didSelectDraftList:(NSIndexPath *)indexPath {
    
    WZProcedureDraft *draft = self.dataArray[indexPath.row];
    [self.dataArray enumerateObjectsUsingBlock:^(WZProcedureDraft *obj, NSUInteger idx, BOOL *stop) {
        if (indexPath.row == idx) {
            obj.isSelected = YES;
        } else {
            obj.isSelected = NO;
        }
    }];
    
    [self.rightTableView reloadData];
    // 草稿箱
    NSString *urlStr = [[NSString stringWithFormat:@"http://%@/OA_CBD%@&workapplytype=apply&formid=%@&fversion=%@&dataid=%@&olddataid=%@&workFlowflag&taskdef=%@&mversion=%@",SERVERIP, self.editUrl, draft.formid, draft.fversion, draft.old_dataid, draft.modelid, draft.taskdef, draft.mversion] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    NSString *autho = [[[NSString stringWithFormat:@"%@:%@:%@",self.oaUserName,self.oaPwd,self.oaCompanyID] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
    [req addValue:[NSString stringWithFormat:@"Basic %@",autho] forHTTPHeaderField:@"Authorization"];
    
    NSURLRequest *request = [req copy];
    WZWebViewController *web = [[WZWebViewController alloc] init];
    web.proDraft = draft;
    web.request = request;
    [self.navigationController pushViewController:web animated:YES];
}



#pragma mark - SWTableViewDelegate
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
    NSIndexPath *indexPath = [self.rightTableView indexPathForCell:cell];
    WZProcedureDetail *detail = self.dataArray[indexPath.row];
    switch (index) {
        case 0:
        {
            if (self.seletedBtn.tag == 30007) {
                
            [MBProgressHUD showMessage:nil toView:self.view];
            WZProcedureDraft *draft = self.dataArray[indexPath.row];
                
            [NetWorkShare delDraftWithUrlStr:self.delUrl draftId:draft.draftId loginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
                
                if (responseObject) {
                    
                    [self shuaXinShuJu];
                    
                } else {
                    
                    [MBProgressHUD showError:NETWORKNERROR toView:self.view];
                }
                
            } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showError:@"删除失败"];
            }];
                
            } else { // 查看日志
                WZProcedureLogsController *log =  [[WZProcedureLogsController alloc] init];
                log.processInstId = detail.procInsId;
                log.oaUserName = self.oaUserName;
                log.oaUserId = self.oaUserId;
                log.oaPwd = self.oaPwd;
                log.oaCompanyID = self.oaCompanyID;
                log.oaName = self.oaName;
                
                [self.navigationController pushViewController:log animated:YES];
            }
            
            break;
        }
        default:
            break;
    }
}

- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell
{
    // allow just one cell's utility button to be open at once
    return YES;
}

- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state
{
    switch (state) {
        case 1:
            // set to NO to disable all left utility buttons appearing
            return NO;
            break;
        case 2:
            // set to NO to disable all right utility buttons appearing
            return YES;
            break;
        default:
            break;
    }
    
    return YES;
}



//添加子控件
- (void)addChildControl {
    
    //创建左侧line
    UIView *shortLine = [[UIView alloc] init];
    shortLine.backgroundColor = RGBCOLORWITH16COLOR(0xe5e5e5);
    [self.view addSubview:shortLine];
    self.leftLine = shortLine;
    
    //创建左侧table
    WZMenuView *menuView = [[WZMenuView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    menuView.backgroundColor = ClearColor;
    menuView.menueViewDelegate = self;
    menuView.showsVerticalScrollIndicator = NO;
    menuView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:menuView];
    self.leftTableView = menuView;
    
    
    //创建右侧table
    UITableView *rightView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    rightView.backgroundColor = ClearColor;
    rightView.delegate = self;
    rightView.dataSource = self;
    rightView.showsVerticalScrollIndicator = NO;
    rightView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:rightView];
    self.rightTableView = rightView;
    
    
    CGFloat btnH = OppositeHeightRate(47);
    CGFloat btnW = self.view.width / 4.0;
    CGFloat btnX = 0;
    CGFloat btnY = OppositeHeightRate(25);
    
    UIImageView *bottomZankaiView = [[UIImageView alloc] init];
    bottomZankaiView.userInteractionEnabled = YES;
    bottomZankaiView.image = [UIImage imageNamed:@"bottombeijing"];
    bottomZankaiView.frame = CGRectMake(0, HARDWARE_SCREEN_HEIGHT - 64 - OppositeHeightRate(72), self.view.width, OppositeHeightRate(72));
    [self.view addSubview:bottomZankaiView];
    self.bottomZankaiView = bottomZankaiView;
    
    //    WZTabBarButton *banjie = [self addtableBarButtonWithImageName:@"banjieliuchengNormal" selectedImageName:@"banjieliucheng" title:@"办结流程"];
    //    banjie.tag = 30006;
    //    [bottomZankaiView addSubview:banjie];
    
    WZTabBarButton *caogao = [self addtableBarButtonWithImageName:@"caogaoxiangNormal" selectedImageName:@"caogaoxiang" title:@"草稿箱"];
    caogao.tag = 30007;
    [bottomZankaiView addSubview:caogao];
    
    for (int i = 0; i < bottomZankaiView.subviews.count; i++) {
        WZTabBarButton *btn = bottomZankaiView.subviews[i];
        btnX = i * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    
    WZTabBarButton *guanbi = [WZTabBarButton buttonWithType:UIButtonTypeCustom];
    guanbi.tag = 30008;
    guanbi.frame = CGRectMake(0, 0, 35.0f, 35.0f);
    guanbi.center = CGPointMake(bottomZankaiView.width / 2.0f, OppositeHeightRate(22));
    [guanbi setBackgroundImage:[UIImage imageNamed:@"guanbianniu_zankaizhuangtai"] forState:UIControlStateNormal];
    [guanbi setBackgroundImage:[UIImage imageNamed:@"guanbianniu_zankaizhuangtai"] forState:UIControlStateSelected];
    [guanbi addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomZankaiView addSubview:guanbi];
    
    //创建底部工具栏
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = ClearColor;
    bottomView.frame = CGRectMake(0, HARDWARE_SCREEN_HEIGHT - 64 - OppositeHeightRate(72), self.view.width, OppositeHeightRate(72));
    [self.view addSubview:bottomView];
    
    UIImageView *bottomtuqiBeijing = [[UIImageView alloc] init];
    bottomtuqiBeijing.userInteractionEnabled = YES;
    bottomtuqiBeijing.image = [UIImage imageNamed:@"bottombeijing"];
    bottomtuqiBeijing.frame = bottomView.bounds;
    [bottomView addSubview:bottomtuqiBeijing];
    self.bottomTUqiBeijing = bottomtuqiBeijing;
    
    
    UIImageView *bottomJuXingBeijing = [[UIImageView alloc] init];
    bottomJuXingBeijing.userInteractionEnabled = YES;
    bottomJuXingBeijing.image = [UIImage imageNamed:@"bottomjuxingbeijing"];
    bottomJuXingBeijing.frame = CGRectMake(0, OppositeHeightRate(22), self.view.width, OppositeHeightRate(50));
    [bottomView addSubview:bottomJuXingBeijing];
    bottomJuXingBeijing.hidden = YES;
    self.bottomJuxingBeijing = bottomJuXingBeijing;
    
    
    WZTabBarButton *shenqing = [self addtableBarButtonWithImageName:@"liuchengshenqingNormal" selectedImageName:@"liuchengshenqing" title:@"流程申请"];
    shenqing.tag = 30001;
    
    shenqing.frame = CGRectMake(0, btnY, btnW, btnH);
    [bottomView addSubview:shenqing];
    
    WZTabBarButton *yishenqing = [self addtableBarButtonWithImageName:@"yishenqingliuchengNormal" selectedImageName:@"yishenqingliucheng" title:@"已申请流程"];
    yishenqing.tag = 30002;
    btnX = 1 * btnW;
    yishenqing.frame = CGRectMake(btnX, btnY, btnW, btnH);
    [bottomView addSubview:yishenqing];
    
    WZTabBarButton *daiban = [self addtableBarButtonWithImageName:@"daibanliuchengnormal" selectedImageName:@"daibanliucheng" title:@"待办流程"];
    daiban.tag = 30003;
    btnX = 2 * btnW;
    daiban.frame = CGRectMake(btnX, btnY, btnW, btnH);
    [bottomView addSubview:daiban];
    
//    CGRect rect = CGRectMake(10, 0, 20, 20);
//    UIImageView *badgeView = [[UIImageView alloc] initWithFrame:rect];
    UIImageView *badgeView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"badge"]];
    self.badgeView = badgeView;
    badgeView.hidden = YES;
    [daiban addSubview:badgeView];
    [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(15);
        make.top.equalTo(daiban.mas_top).offset(1);
        make.left.equalTo(daiban.mas_right).offset(-28);
    }];
    
    UILabel *badgeLabel = [[UILabel alloc] init];
    self.badgeLabel = badgeLabel;
    badgeLabel.font = [UIFont systemFontOfSize:10];
    badgeLabel.textColor = WhiteColor;
    badgeLabel.textAlignment = NSTextAlignmentCenter;
    [badgeView addSubview:badgeLabel];
    [badgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(badgeView.mas_left).offset(2);
        make.top.equalTo(badgeView.mas_top).offset(2);
//        make.width.mas_equalTo(10);
    }];
    [badgeLabel sizeToFit];

    
    WZTabBarButton *yiban = [self addtableBarButtonWithImageName:@"yibanliucheng" selectedImageName:@"yibanliuchengNormal" title:@"已办流程"];
    yiban.tag = 30004;
    btnX = 3 * btnW;
    yiban.frame = CGRectMake(btnX, btnY, btnW, btnH);
    [bottomView addSubview:yiban];
    
    
    WZTabBarButton *zankai = [WZTabBarButton buttonWithType:UIButtonTypeCustom];
    zankai.tag = 30005;
    zankai.frame = CGRectMake(0, 0, 35.0f, 35.0f);
    zankai.center = CGPointMake(bottomView.width / 2.0f, OppositeHeightRate(22));
    [zankai setBackgroundImage:[UIImage imageNamed:@"guanbianniu"] forState:UIControlStateNormal];
    [zankai setBackgroundImage:[UIImage imageNamed:@"guanbianniu"] forState:UIControlStateSelected];
    [zankai addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:zankai];
    self.zankai = zankai;
    
    self.leftTableView.frame = CGRectMake(0, 0, OppositeWidthRate(64), bottomView.y);
    self.rightTableView.frame = CGRectMake(CGRectGetMaxX(self.leftTableView.frame), 0, self.view.width - CGRectGetMaxX(self.leftTableView.frame), bottomView.y);
    shortLine.frame = CGRectMake(menuView.width - 0.5f, 0, 0.5f, HARDWARE_SCREEN_HEIGHT - 64 - OppositeHeightRate(50));
    
    [self btnClick:shenqing];
    
}



- (void)fetchPendingData
{

    [NetWorkShare daiBanLiuChengWithLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID pageNo:@"1" pageSize:@"20" category:nil successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                NSArray *pendingArray = [WZProcedureDetail objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                
                NSString * str = [self transformJsonStrWithObject:responseObject[@"datas"]];
                WZLog(@"fetch Data= %@", str);
                
                if (!pendingArray.count) {
                    self.badgeView.hidden = YES;
                } else {
                    self.badgeLabel.text = (pendingArray.count > 9) ? [NSString stringWithFormat:@"%@", @"9+"] : [NSString stringWithFormat:@"%zd", pendingArray.count];
                    self.badgeView.hidden = NO;
                }
            
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}



#pragma mark 懒加载
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)listArray {
    
    if (_listArray == nil) {
        _listArray = [[NSMutableArray alloc] init];
    }
    return _listArray;
}

- (NSString *)transformJsonStrWithObject:(NSDictionary *)obj {
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:0 error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return jsonStr;
}

@end
